home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 101 / CD-ROM 101.iso / compl / maya5ple / Install_MayaPLE5_English.exe / Maya / Data1.cab / scriptNodes.mel < prev    next >
Encoding:
Text File  |  2003-07-17  |  14.8 KB  |  575 lines

  1. // Copyright (C) 1997-2002 Alias|Wavefront,
  2. // a division of Silicon Graphics Limited.
  3. //
  4. // The information in this file is provided for the exclusive use of the
  5. // licensees of Alias|Wavefront.  Such users have the right to use, modify,
  6. // and incorporate this code into other products for purposes authorized
  7. // by the Alias|Wavefront license agreement, without fee.
  8. //
  9. // ALIAS|WAVEFRONT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  10. // INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  11. // EVENT SHALL ALIAS|WAVEFRONT BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  12. // CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  13. // DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  14. // TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  15. // PERFORMANCE OF THIS SOFTWARE.
  16. //
  17. //
  18. //  Alias|Wavefront Script File
  19. //  MODIFY THIS AT YOUR OWN RISK
  20. //
  21. //  ==================== expressionEdTextEditor.mel ==========
  22. //
  23. //  SYNOPSIS
  24. //         Procedures used for script nodes in the Expression Editor.
  25. //
  26. //  CONTENTS
  27. //        (The procs are in the file in the order listed here.)
  28. //
  29. //  EEgetScriptList            Get/return list of all scripts in the scene.
  30. //  EEisScriptNode            Is the node a script node?
  31. //  EEscriptNameExists        Does user-typed script node name exist?
  32. //  EEcheckValidScriptName    Is user-typed script node name valid?
  33. //    EEgetScriptNodeAttr        Returns the script from the script node.
  34. //    EEapplyScript            Sets the script to the script node.
  35. //    EErestoreScript            Restore the current script from the node.
  36. //    EEscriptListChanged        User selected a new script node.
  37. //    EEgetFullScriptName        Return the script node and attribute.
  38. //    EEdisplayScript            Display a script.
  39. //    EEupdateFileScript        Update the script window when an editor saves 
  40. //                            the script.
  41. //
  42. //    EEselectedScriptCB        Callback for script node name textfield.
  43. //    EEbeforeAfterScriptCB    Callback for the before/after radio button.
  44. //    EEscriptNodeTestCB        Callback for the test button.
  45. //    
  46. // Special note for scriptNodes: The name stored in the $EEcurrFileExprName
  47. // array is not simply the node name. Since a scriptNode has two different
  48. // scripts, additional information is needed to determine if the script
  49. // is a "before" script or an "after" script. The
  50. // EEgetFullScriptName(string $nodeName) procedure should be used to create
  51. // a unique name, and everytime the full scriptName needs to be used.
  52.  
  53. //  ================ EEgetScriptList ================
  54. //
  55. //
  56. //  SYNOPSIS
  57. //     Get and return the list of all scripts in the scene.
  58. //
  59. global proc string[] EEgetScriptList()
  60. {
  61.     string $nodeList[];
  62.     string $scriptList[];
  63.  
  64.     // Get all the script nodes.
  65.     //
  66.     $nodeList = `ls -type script`;
  67.  
  68.     return $nodeList;
  69.  
  70. }    // EEgetScriptList
  71.  
  72.  
  73. //  ================ EEisScriptNode ================
  74. //
  75. //  SYNOPSIS
  76. //      Return whether the object is a script node
  77. //
  78. global proc int EEisScriptNode(string $nodeName)
  79. {
  80.     if (size(`ls -type script $nodeName`) != 0) {
  81.         return 1;
  82.     }
  83.     return 0;
  84.  
  85. }    // EEisScriptNode
  86.  
  87.  
  88. //  ================ EEscriptNameExists ================
  89. //
  90. //  SYNOPSIS
  91. //      Return if the name is of an existing script
  92. //
  93. global proc int EEscriptNameExists(string $scriptName)
  94. {
  95.     string $allScriptNames[] = EEgetScriptList();
  96.     int $i, $numScripts;
  97.     $numExpressions = size($allScriptNames);
  98.  
  99.     int $nameExists = 0;
  100.  
  101.     for ($i = 0; $i < $numExpressions; $i++)
  102.     {
  103.         if ($allScriptNames[$i] == $scriptName)
  104.         {
  105.             $nameExists = 1;
  106.             break;
  107.         }
  108.     }
  109.     clear($allScriptNames);
  110.  
  111.     return $nameExists;
  112. }    // EEscriptNameExists
  113.  
  114.  
  115. //  ================ EEcheckValidScriptName ================
  116. //
  117. //  SYNOPSIS
  118. //      Make sure the script name sent in is valid.
  119. //
  120. global proc int EEcheckValidScriptName(string $scriptName)
  121. {
  122.     global string $EEcurrExpressionName;
  123.     int $nameValid = 1;
  124.  
  125.     // The user can only name a new script or change the
  126.     // name of an script being edited;  so if the name
  127.     // the user typed is already the name of an script node
  128.     // then it is not valid, so remove it and restore the
  129.     // previous name.
  130.     //
  131.     int $nameExists = EEscriptNameExists($scriptName);
  132.  
  133.     if ($nameExists)
  134.     {
  135.         $nameValid = 0;
  136.         warning ("Expression Editor: name "+$scriptName+" is already used. Restoring previous name.");
  137.  
  138.         // Name already exists, so is not valid to reuse, so restore the
  139.         // previous name, or blank the field if there was no name.
  140.         //
  141.         if (size($EEcurrExpressionName) > 0)
  142.             textField -e -tx $EEcurrExpressionName EEexprNameT;
  143.         else
  144.             textField -e -tx "" EEexprNameT; 
  145.     }
  146.  
  147.     return $nameValid;
  148.  
  149. }    // EEcheckValidScriptName
  150.  
  151.  
  152. //  ================ EEgetScriptNodeAttr ================
  153. //
  154. //  SYNOPSIS
  155. //      Sets the script to the script node
  156. //
  157. proc string EEgetScriptNodeAttr()
  158. {
  159.     string $attrName = `textFieldGrp -q -tx EEselScriptNodeNameT`;
  160.     if (`size($attrName)` == 0) {
  161.         return "";
  162.     }
  163.  
  164.     int $sel = `radioButtonGrp -q -sl EEscriptRBG`;
  165.     if ($sel == 1) {
  166.         $attrName += ".before";
  167.     } else {
  168.         $attrName += ".after";
  169.     }
  170.  
  171.     return $attrName;
  172. }    // EEgetScriptNodeAttr
  173.  
  174.  
  175. //  ================ EEapplyScript ================
  176. //
  177. //  SYNOPSIS
  178. //      Sets the script to the correct attribute of the scriptNode.
  179. //
  180. global proc string EEapplyScript(string $script)
  181. {
  182.     global string $EEnodeMode;
  183.     global int $EEcreateMode;
  184.     global string $EEcurrExpressionName;
  185.     global string $EEorigExpressionName;
  186.  
  187.     if (!$EEcreateMode) 
  188.     {
  189.         string $attrName = EEgetScriptNodeAttr();
  190.         if (`size($attrName)` == 0) {
  191.             return "";
  192.         }
  193.         setAttr -type "string" $attrName $script;
  194.  
  195.         return "";
  196.     }
  197.  
  198.     //    A new scriptNode should be created.
  199.     //
  200.     string $newScriptName, $currScriptName;
  201.     string $scriptFlag = " -beforeScript ";
  202.  
  203.     int $beforeAfter = `radioButtonGrp -q -sl EEscriptRBG`;
  204.     if ($beforeAfter != 1) 
  205.     {
  206.         $scriptFlag = " -afterScript ";
  207.     } 
  208.  
  209.     // If $currScriptName != $currExpressionName, it means
  210.     // the new name has not yet been processed or validated.
  211.     // (This can happen if the user does not press carriage
  212.     // return, and then selects "Apply", because there is
  213.     // no focus-out callback available in ELF.)
  214.     //
  215.     $currScriptName = `textField -q -tx EEexprNameT`;
  216.     if (size($currScriptName) > 0 &&
  217.         $currScriptName != $EEcurrExpressionName)
  218.     {
  219.         if (EEcheckValidScriptName($currScriptName)) 
  220.             $EEcurrExpressionName = $currScriptName;
  221.     }
  222.  
  223.     string $cmd = "scriptNode ";
  224.     if (`size($currScriptName)` == 0 &&
  225.         `size($EEcurrExpressionName)` == 0)
  226.     {
  227.         $cmd += ($scriptFlag + " \"" + encodeString($script) + "\"");
  228.     }
  229.     else 
  230.     {
  231.         $cmd += ($scriptFlag + " \"" + encodeString($script) + "\" -n " + 
  232.                 $EEcurrExpressionName);
  233.     }
  234.  
  235.     string $newScriptName = evalEcho($cmd);
  236.     if (size($newScriptName) > 0) {
  237.         EEresetExpressionName($newScriptName);
  238.         EEsetEditMode("Editing Script");
  239.  
  240.         $EEcurrExpressionName = $newScriptName;
  241.         $EEorigExpressionName = $newScriptName;
  242.  
  243.         attrEnumOptionMenu -e -at ($newScriptName + ".st") EEscriptNodeTypeAOM;
  244.         textFieldGrp -e -tx $newScriptName EEselScriptNodeNameT;
  245.  
  246.         textScrollList -e -da EEnodeList;
  247.         EEselectNodeInList($EEcurrExpressionName);
  248.     }
  249.  
  250.     return $newScriptName;
  251. }     // EEapplyScript
  252.  
  253.  
  254. //  ================ EErestoreScript ================
  255. //
  256. //  SYNOPSIS
  257. //      Restore the current script to the editor 
  258. //
  259. //
  260. global proc EErestoreScript()
  261. {
  262.     string $attr = EEgetScriptNodeAttr();
  263.     string $script = `getAttr $attr`;
  264.     scrollField -e -tx $script EEmultiText;
  265.  
  266. }    // EErestoreScript
  267.  
  268.  
  269. //  ================ EEscriptListChanged ================
  270. //
  271. //  SYNOPSIS
  272. //     User has selected a new script node in the node list. 
  273. //
  274. global proc EEscriptListChanged(string $scriptNode)
  275. {
  276.     global string $EEcurrSelectedNode;
  277.     global string $EEcurrExpressionName;
  278.     global string $EEorigExpressionName;
  279.     global int $EEcurrentEditor;
  280.     global int $EEdoLaunchTextEd;
  281.  
  282.     // Here and below have to take into account that the user may
  283.     // have clicked (esp. double-clicked) on the node that is 
  284.     // currently already in the editor.  In that case, the only
  285.     // thing that needs to be done is launch a text editor, if the
  286.     // the user double-clicked.
  287.     //
  288.     if ($scriptNode != $EEcurrSelectedNode)
  289.     {
  290.         EEclearAllControls();
  291.         EEresetNodeControls($scriptNode);
  292.         EEdisplayScript($scriptNode);
  293.     }
  294.  
  295.     // If the user has double-clicked on the node, launch an editor.
  296.     //
  297.     string $fullScriptName = EEgetFullScriptName($scriptNode);
  298.     int    $exprInEditor = EEexpressionIsInTextEditor($fullScriptName, 0);
  299.     if ($EEdoLaunchTextEd && $EEcurrentEditor != 1 && $exprInEditor == -1)
  300.     {
  301.         string $script = `getAttr $fullScriptName`;
  302.         EElaunchEditor($fullScriptName, $script, $fullScriptName);
  303.  
  304.         scrollField -e -enable false EEmultiText;
  305.     }
  306.  
  307. }    // EEscriptListChanged
  308.  
  309.  
  310. //  ================ EEgetFullScriptName ================
  311. //
  312. //
  313. //  SYNOPSIS
  314. //        The name of the node is not a unique script ID. The value of
  315. //        of the EEscriptRBG radioButtonGrp should also be taken into
  316. //        account.
  317. //
  318. global proc string EEgetFullScriptName(string $scriptNode)
  319. {
  320.     //    Make sure a full name was not already passed.
  321.     //
  322.     string $buffer[];
  323.     tokenize($scriptNode, ".", $buffer);
  324.  
  325.     if (size($buffer) == 2 || size($scriptNode) == 0) 
  326.     {
  327.         return $scriptNode;
  328.     }
  329.  
  330.     string $attrName;
  331.     if (`radioButtonGrp -q -sl EEscriptRBG` == 1) {
  332.         $attrName = "before";
  333.     } else {
  334.         $attrName = "after";
  335.     }
  336.  
  337.     return ($buffer[0] + "." + $attrName);
  338. }     // EEgetFullScriptName
  339.  
  340.  
  341. //  ================ EEdisplayScript ================
  342. //
  343. //  SYNOPSIS
  344. //        Display a script node script.
  345. //
  346. global proc EEdisplayScript(string $scriptNode)
  347. {
  348.     global int $EEdoLaunchTextEd;
  349.     global int $EEcurrentEditor;
  350.     global int $EEexpressionInEditor;
  351.     global string $EEorigExpressionName;
  352.     global string $EEcurrExpressionName;
  353.  
  354.     if ($scriptNode == "") 
  355.     {
  356.         return;
  357.     }
  358.  
  359.     //    Set the data into the exitor controls.
  360.     //
  361.     text -e -enable true EEexprNameLabel;
  362.     EEresetExpressionName($scriptNode);
  363.     textField -e -enable true EEexprNameT;
  364.  
  365.     string $fullScriptName = EEgetFullScriptName($scriptNode);
  366.     string $script = `getAttr $fullScriptName`;
  367.  
  368.     scrollField -e -tx $script EEmultiText;
  369.     int $scriptInEditor = 
  370.         EEexpressionIsInTextEditor($fullScriptName, 0);
  371.  
  372.     if ($EEcurrentEditor != 1 && $EEdoLaunchTextEd && $scriptInEditor == -1)
  373.     {
  374.         EElaunchEditor("", $script, $fullScriptName);
  375.         scrollField -e -enable false EEmultiText;
  376.     }
  377.     else 
  378.     {
  379.         if ($scriptInEditor == -1)
  380.         {
  381.             $EEexpressionInEditor = -1;
  382.             scrollField -e -enable true EEmultiText;
  383.         }
  384.         else 
  385.         {
  386.             $EEexpressionInEditor = $scriptInEditor;
  387.             scrollField -e -enable false EEmultiText;
  388.         }
  389.     }
  390.  
  391.     $EEdoLaunchTextEd = 0;
  392.     $EEcurrExpressionName = $scriptNode;
  393.     $EEorigExpressionName = $scriptNode;
  394.      
  395.     EEsetEditMode("Editing Script Node");
  396. }     // EEdisplayScript
  397.  
  398.  
  399. //  ================ EEupdateFileScript ================
  400. //
  401. //  SYNOPSIS
  402. //      A text editor file has been written.  So get its contents
  403. //        and update the script and the Expression Editor.
  404. //
  405. global proc    EEupdateFileScript(string $fileName, 
  406.                                    string $script, int $isCurrent)
  407. {
  408.     global string $EEcurrFiles[];
  409.     global string $EEcurrFileExprName[];
  410.     global string $EEcurrFileDefObj[];
  411.     global int      $EEcurrFileCreateMode[];   // 1 = creating; 0 = editing
  412.     global int $EEeditedInEditor;
  413.  
  414.     // Find the file in the list.
  415.     //
  416.     int $i, $index = -1;
  417.  
  418.     for ($i = 0; $i < size($EEcurrFiles); $i++)
  419.     {
  420.         if ($fileName == $EEcurrFiles[$i])
  421.         {
  422.             $index = $i;
  423.             break;
  424.         }
  425.     }
  426.  
  427.     if ($index == -1)
  428.         return;
  429.  
  430.     // Find out if editing or creating this script.
  431.     //
  432.     int $creating = $EEcurrFileCreateMode[$index];
  433.  
  434.     // Make sure the script can get through MEL.
  435.     //
  436.     string $newScriptNode;
  437.  
  438.     if ($creating)
  439.     {
  440.         // If creating and the script is currently in the Expression
  441.         // Editor, need to do other flags as well, so call the same
  442.         // procedure as when selecting the create button.
  443.         // Otherwise just edit the script itself..
  444.         //
  445.         // Don't try to make an script node if there's no script.
  446.         //
  447.         if ($script == "")
  448.             return;
  449.  
  450.         string $currScriptName;
  451.         string $scriptFlag = " -beforeScript ";
  452.  
  453.         int $beforeAfter = `radioButtonGrp -q -sl EEscriptRBG`;
  454.         if ($beforeAfter != 1)
  455.         {
  456.             $scriptFlag = " -afterScript ";
  457.         }
  458.  
  459.         $currScriptName = `textField -q -tx EEexprNameT`;
  460.  
  461.         string $cmd = "scriptNode ";
  462.         if ((size($currScriptName)) > 0)
  463.         {
  464.             if (EEcheckValidScriptName($currScriptName))
  465.             {
  466.                 $cmd += (" -n " + $currScriptName + " ");
  467.             }
  468.         }
  469.         $cmd += ($scriptFlag + " \"" + encodeString($script) + "\"");
  470.         $newScriptNode = evalEcho($cmd);
  471.  
  472.         // No longer in create mode.
  473.         //
  474.         $EEcurrFileCreateMode[$index] = 0;
  475.         $EEcurrExpressionName = $newScriptNode;
  476.  
  477.         $EEcurrFileExprName[$index] = EEgetFullScriptName($newScriptNode);
  478.     }
  479.     else
  480.     {
  481.         // Editing.  So just edit the script, and update the
  482.         // Expression Editor scroll field.
  483.         //
  484.         string $cmd = 
  485.             "setAttr -type \"string\" " + $EEcurrFileExprName[$index] +
  486.             " \"" + encodeString($script) + "\"";
  487.         evalEcho($cmd);
  488.  
  489.         if ($isCurrent)
  490.         {
  491.             $EEeditedInEditor = 1;
  492.             scrollField -e -text $script EEmultiText;
  493.         }
  494.     }
  495.  
  496. }    // EEupdateFileScript
  497.  
  498.  
  499. //  ================ EEselectedScriptCB ================
  500. //
  501. //  SYNOPSIS
  502. //      Called when the selected script node name textfield 
  503. //        is changed.
  504. //
  505. global proc EEselectedScriptCB()
  506. {
  507.     global string $EEnodeMode;
  508.  
  509.     if ($EEnodeMode != "scriptNode")
  510.     {
  511.         return;
  512.     }
  513.  
  514.     string $nodeName = `textFieldGrp -q -tx EEselScriptNodeNameT`;
  515.     if (size($nodeName) == 0) 
  516.     {
  517.         //    An empty textfield, so deselect everything.
  518.         //
  519.         EEclearAllControls();
  520.  
  521.         textScrollList -e -da EEnodeList;
  522.         return;
  523.     }
  524.  
  525.     EEclearAllControls();
  526.     if (EEnodeIsInList($nodeName) > -1)
  527.     {
  528.         EEnewSelectedNode($nodeName);
  529.         attrEnumOptionMenu -e -at ($nodeName + ".st") EEscriptNodeTypeAOM;
  530.     }
  531.     else 
  532.     {
  533.         //    Reset the textfield with the current object name,
  534.         //    if there is one.
  535.         //
  536.         string $names[];
  537.         $names = `textScrollList -q -si EEnodeList`;
  538.         $nodeName = $names[0];
  539.     }
  540.  
  541.     //    Re-write the textfield with the nodeName.
  542.     //
  543.     textFieldGrp -e -tx $nodeName EEselScriptNodeNameT;
  544.     EEdisplayScript($nodeName);
  545.  
  546. }     // EEselectedScriptCB
  547.  
  548.  
  549. //  ================ EEbeforeAfterScriptCB ================
  550. //
  551. //  SYNOPSIS
  552. //     Called when the before/after radio button is selected.
  553. //
  554. //
  555. global proc EEbeforeAfterScriptCB()
  556. {
  557.     string $scriptNode = `textFieldGrp -q -tx EEselScriptNodeNameT`;
  558.  
  559.     EEclearAllControls();
  560.     EEresetNodeControls($scriptNode);
  561.     EEdisplayScript($scriptNode);
  562. }     // EEbeforeAfterScriptCB
  563.  
  564.  
  565. //  ================ EEscriptNodeTestCB ================
  566. //
  567. //  SYNOPSIS
  568. //      Called when the script node type option menu changes.
  569. //
  570. global proc EEscriptNodeTestCB()
  571. {
  572.     string $theText = `scrollField -q -tx EEmultiText`;
  573.     eval($theText);
  574. }    //    EEscriptNodeTestCB
  575.